package com.ruoyi.mall.app.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.app.service.DtsAddressService;
import com.ruoyi.mall.app.service.DtsRegionService;
import com.ruoyi.mall.common.domain.DtsAddress;
import com.ruoyi.mall.common.domain.DtsRegion;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/user/address")
public class DtsAddressController extends BaseController {

    @Autowired
    private DtsAddressService dtsAddressService ;
    @Autowired
    private DtsRegionService dtsRegionService ;

    //查询地址
    @GetMapping("/addressList")
    public AjaxResult addressList(int userId)
    {
        List<DtsAddress> list = dtsAddressService.selectAddress(userId);
        if (list != null){
            return success(list);
        }else {
            return error();
        }
    }

    @GetMapping("/addressIsDefault")
    public AjaxResult addressIsDefault(int userId)
    {
        DtsAddress dtsAddress = dtsAddressService.selectDefaultAddress(userId);
        if (dtsAddress != null){
            return success(dtsAddress);
        }else {
            return error();
        }
    }


    //新增地址
    @GetMapping("/addressInsert")
    public AjaxResult add(DtsAddress address)
    {
        return toAjax(dtsAddressService.addAddress(address));
    }

    //修改地址
    @GetMapping("/addressUpdate")
    public AjaxResult edit(DtsAddress dtsaddress)
    {
        return toAjax(dtsAddressService.upAddress(dtsaddress));
    }

    //删除地址
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Long id)
    {
        dtsAddressService.delAddress(id);
        return success();
    }

    // 删除地址
    @GetMapping("/delAddress")
    public AjaxResult delAddress(DtsAddress dtsAddress) {
        return toAjax(dtsAddressService.delAddress1(dtsAddress));
    }

    //行政区域 省
    @GetMapping("/provinces")
    public AjaxResult getProvinces() {
        List<DtsRegion> provinces = dtsRegionService.getProvinces();
        if (provinces != null) {
            return success(provinces);
        } else {
            return error();
        }
    }
    //行政区域 市
    @GetMapping("/cities")
    public AjaxResult getCitiesByProvince(@RequestParam Long provinceId) {
        List<DtsRegion> cities = dtsRegionService.getCitiesByProvince(provinceId);
        if (cities != null) {
            return success(cities);
        } else {
            return error();
        }
    }
    //行政区域 区
    @GetMapping("/districts")
    public AjaxResult getDistrictsByCity(@RequestParam Long cityId) {
        List<DtsRegion> districts = dtsRegionService.getDistrictsByCity(cityId);
        if (districts != null) {
            return success(districts);
        } else {
            return error();
        }
    }

}
